##  This file contains the code to replicate the analyses reported in Table SI-2
##

rm(list = ls(all.names = TRUE))
library(Hmisc)
library(lmtest)
library(sandwich)
library(pscl)
library(MASS)
library(mice)
library(miceadds)
library(openxlsx)


#setwd("~/Dropbox/Current projects/Nazi trials/analysis")
load("Nazi trials replication data.RData")
file.info("Nazi trials replication data.RData")$mtime


##  how many verdicts changed after appeal?
table(cases.temp$convicted, cases.temp$convicted.ultimate)



##  source helper functions
source("probit2.r")



##
##  Estimate models and save the results
tab1 <- matrix("",29,6)
colnames(tab1) <- paste0("(",1:ncol(tab1),")")

rownames(tab1) <- c(
					"% judges who were appointed in 1933--45", "",
					"% judges who were appointed pre-1933", "",
					"% judges who completed legal training in 1933--45", "",
					"% judges who completed legal training pre-1933", "",
					"avg. share of judges' legal education in 1933--45", "",
					"avg. share of judges' legal education pre-1933", "",
					"% prosecutors who were appointed in 1933--45", "",
					"% prosecutors who were appointed pre-1933", "",
					"% prosecutors who completed legal training in 1933--45", "",
					"% prosecutors who completed legal training pre-1933", "",
					"avg. share of prosecutors' legal education in 1933--45", "",
					"avg. share of prosecutors' legal education pre-1933", "",
					"Year FE","State FE", "Crime categorization FE",
					"$p$-value Wald test % judges","$p$-value Wald test % prosecutors")

sims.tab <- vector(mode = "list", length = 6)


##  add additional predictors?
add.pred <- " + state"	#  state FEs; all models include crime categories and year FEs



##  col 1
add.pred.MI <- c("NS_judge_@_20","NS_prosecutor_@_20")

##  probit model
out <- glm.MI(d = cases.temp, outcome = "convicted.ultimate", add.pred.MI = add.pred.MI, add.pred = add.pred,
sims = 1, wald = NULL)
tab1[c(1,13),1] <- out[[1]][2:3,1]
tab1[c(2,14),1] <- out[[1]][2:3,2]
sims.tab[[1]][1] <- list(out[[3]])



##  col 2
add.pred.MI <- c("NS_judge_@_20","NS_prosecutor_@_20","NS_judge_@_19","NS_prosecutor_@_19")

##  probit
out <- glm.MI(d = cases.temp, outcome = "convicted.ultimate", add.pred.MI = add.pred.MI, add.pred = add.pred,
sims = 1, wald = list(c(1,3),c(2,4)))
tab1[c(1,13,3,15),2] <- out[[1]][2:5,1]
tab1[c(2,14,4,16),2] <- out[[1]][2:5,2]
tab1[28,2] <- out[[2]][1]
tab1[29,2] <- out[[2]][2]
sims.tab[[2]][1] <- list(out[[3]])

out <- glm.MI(d = cases.temp, outcome = "convicted.ultimate", add.pred.MI = add.pred.MI, add.pred = add.pred,
sims = 3, wald = list(c(1,3),c(2,4)))
sims.tab[[2]][2] <- list(out[[3]])



##  col 5
add.pred.MI <- c("NS_judge_@_24","NS_prosecutor_@_24")

##  probit
out <- glm.MI(d = cases.temp, outcome = "convicted.ultimate", add.pred.MI = add.pred.MI, add.pred = add.pred,
sims = 1, wald = NULL)
tab1[c(9,21),5] <- out[[1]][2:3,1]
tab1[c(10,22),5] <- out[[1]][2:3,2]
sims.tab[[5]][1] <- list(out[[3]])



##  col 6
add.pred.MI <- c("NS_judge_@_24","NS_prosecutor_@_24","NS_judge_@_23","NS_prosecutor_@_23")

##  probit
out <- glm.MI(d = cases.temp, outcome = "convicted.ultimate", add.pred.MI = add.pred.MI, add.pred = add.pred,
sims = 1, wald = list(c(1,3),c(2,4)))
tab1[c(9,21,11,23),6] <- out[[1]][2:5,1]
tab1[c(10,22,12,24),6] <- out[[1]][2:5,2]
tab1[28,6] <- out[[2]][1]
tab1[29,6] <- out[[2]][2]
sims.tab[[6]][1] <- list(out[[3]])

out <- glm.MI(d = cases.temp, outcome = "convicted.ultimate", add.pred.MI = add.pred.MI, add.pred = add.pred,
sims = 3, wald = list(c(1,3),c(2,4)))
sims.tab[[6]][2] <- list(out[[3]])



##  add checkmarks to table
tab1[25:27,1:6]	<- "\\checkmark"

##  extract only the relevant parts of the results table
tab1[c(1:4,9:16,21:29),c(1:2,5:6)]
latex(tab1[c(1:4,9:16,21:29),c(1:2,5:6)], file = "")


sessionInfo()








.

